package com.envper.security.service.impl;

import com.envper.common.domain.dto.UserDto;
import com.envper.mysql.domain.Status;
import com.envper.common.exception.GlobalException;
import com.envper.common.service.PermService;
import com.envper.common.service.RoleService;
import com.envper.common.service.UserService;
import com.envper.security.domain.LoginUser;
import com.envper.security.service.LoginUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Set;

@Service
@Slf4j
@RequiredArgsConstructor
public class LoginUserServiceImpl implements LoginUserService {
    private final UserService userService;
    private final PermService permService;
    private final RoleService roleService;

    @Override
    public LoginUser getLoginUserByUsername(String username) {
        UserDto userDto = userService.getUserDtoByUsername(username);
        if (ObjectUtils.isEmpty(userDto)) throw new GlobalException(Status.USERNOTFOUND);
        Long userId = userDto.getId();
        Set<String> permList = permService.getPermCodeSetByUserId(userId);
        Set<String> roleList = roleService.getRoleCodeSetByUserId(userId);
        return new LoginUser(userId, permList, roleList, userDto);
    }
}
